﻿using System;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using Microsoft.SharePoint.Utilities;

namespace LifeInSharePoint.Metro.Layouts.LifeInSharePoint.Metro.admin
{
    public partial class Theme : LayoutsPageBase
    {

        private const string LISTNAME = "Metro UI Themes";

        private bool StoredInheritedColourValue;
        private bool StoredGlobalAccordionValue;
        private bool StoredGlobalRibbonValue;
        private string StoredAccordionTypeValue = string.Empty;
        private string StoredColourValue = string.Empty;
        private string StoredQuicklaunchPositionValue = string.Empty;
        private bool StoredAccordionValue;



        protected void Page_Load(object sender, EventArgs e)
        {
            BtnApply.Click += new EventHandler(BtnApply_Click);
            GetPropertyBagValues();
        }



        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            DisableControlChecker();

            //Set Checkbox Value
            if (SPContext.Current.Web.IsRootWeb)
            {
                ApplyAccordionGloballyCB.Checked = StoredGlobalAccordionValue;
            }

            //Set Theme Inheritance Checkbox Value
            InheritThemeFromParent.Checked = StoredInheritedColourValue;

            if (!Page.IsPostBack)
            {
                PopulateThemeDropDown(this.SiteMetroBrandingDDB, StoredColourValue);
                PopulateAccordionTypeDropDown(this.AccordionTypeDropdown, StoredAccordionTypeValue);
                PopulateQuickLaunchPositionDropDown(this.QuickLaunchPosition, StoredQuicklaunchPositionValue);
            }
            //Theme Selector
            setThemeValue(StoredColourValue);

            //Set Accordion Inheritance Checkbox Value
            AccordionCheckbox.Checked = StoredAccordionValue;
            //Set Floated Ribbon Checkbox Value
            FloatRibbonCB.Checked = StoredGlobalRibbonValue;

            if (StoredGlobalAccordionValue)
                AccordionStatusMessage.InnerHtml = "Accordion Settings are Inherited. Please navigate to Site Collection Root to manage. <br />";
        }


        /// <summary>
        /// Gets the Values from the Property Bag to be used within the Admin Page
        /// </summary>
        private void GetPropertyBagValues()
        {
            StoredInheritedColourValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Web, "InheritMetroThemeColour"));

            StoredColourValue = Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Web, "MetroThemeColour");



            //Accordion
            StoredAccordionValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Web, "MetroAccordionActivated"));

            StoredGlobalAccordionValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "GlobalMetroAccordionActivated"));

            StoredGlobalRibbonValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "GlobalMetroFloatedRibbon"));

            StoredQuicklaunchPositionValue = Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "GlobalMetroQuickLaunchPosition");

            StoredAccordionTypeValue = Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Web, "GlobalMetroAccordionType");
        }

        /// <summary>
        /// Enables and Disables Controls on the page based on location and values set in GetPropertyBagValues()
        /// </summary>
        private void DisableControlChecker()
        {

            //If we are on the root site
            if (SPContext.Current.Web.IsRootWeb)
            {
                InheritThemeFromParent.Visible = false;
                AccordionStatusMessage.Visible = false;
            }
            //If we are NOT on the root site
            else
            {
                QuickLaunchPosition.Visible = false;
                
                if (StoredGlobalAccordionValue)
                {
                    AccordionCheckbox.Visible = false;
                    ApplyAccordionGloballyCB.Visible = false;
                    AccordionTypeDropdown.Visible = false;
                    FloatRibbonCB.Visible = false;
                }
            }


            if (StoredInheritedColourValue)
                SiteMetroBrandingDDB.Enabled = false;
        }

        private void setThemeValue(string StoredColourValue)
        {
            try
            {
                if (!string.IsNullOrEmpty(StoredColourValue))
                {
                    ThemeLabel.Text = SPContext.Current.Site.RootWeb.Lists[LISTNAME].GetItemById(Convert.ToInt32(StoredColourValue)).Title;
                }
                else
                {
                    ThemeLabel.Text = "No Theme Set";
                }
            }
            catch (Exception)
            {

                //throw;
            }

        }

        private void PopulateQuickLaunchPositionDropDown(System.Web.UI.WebControls.DropDownList dropDownList, string themeQuicklaunchPosition)
        {
            string[] positions = new string[] { "Right", "Left" };
            foreach (string position in positions)
            {
                dropDownList.Items.Add(position);
            }

            if (string.IsNullOrEmpty(themeQuicklaunchPosition))
            {
                QuickLaunchPositionLabel.Text = positions[0];
            }
            else
            {
                QuickLaunchPositionLabel.Text = themeQuicklaunchPosition;
            }

            for (int i = 0; i < dropDownList.Items.Count; i++)
            {
                if (dropDownList.Items[i].Value == themeQuicklaunchPosition)
                {
                    dropDownList.SelectedIndex = i;
                }
            }
        }

        private void PopulateAccordionTypeDropDown(DropDownList dropDownList, string StoredAccordionValue)
        {
            string[] types = new string[] { "Hover", "Click" };
            foreach (string type in types)
            {
                dropDownList.Items.Add(type);
            }

            if (string.IsNullOrEmpty(StoredAccordionValue))
            {
                QuickLaunchPositionLabel.Text = types[0];
            }
            else
            {
                QuickLaunchPositionLabel.Text = StoredAccordionValue;
            }

            for (int i = 0; i < dropDownList.Items.Count; i++)
            {
                if (dropDownList.Items[i].Value == StoredAccordionValue)
                {
                    dropDownList.SelectedIndex = i;
                }
            }
        }

        private void PopulateThemeDropDown(System.Web.UI.WebControls.DropDownList dropDownList, string themeColour)
        {

            SPList themeList = SPContext.Current.Site.RootWeb.Lists[LISTNAME];
            SPQuery query = new SPQuery();
            query.Query = "<where></where>";
            SPListItemCollection _workCollection = themeList.GetItems(query);
            List<ListItem> colours = new List<ListItem>();
            foreach (SPListItem item in _workCollection)
            {
                ListItem ddItem = new ListItem();
                ddItem.Text = item.Title;
                ddItem.Value = item.ID.ToString();
                colours.Add(ddItem);
            }

            dropDownList.DataSource = colours;
            dropDownList.DataValueField = "Value";
            dropDownList.DataTextField = "Text";
            dropDownList.DataBind();

            for (int i = 0; i < dropDownList.Items.Count; i++)
            {
                if (dropDownList.Items[i].Value == themeColour)
                {
                    dropDownList.SelectedIndex = i;
                }
            }

        }
        void BtnApply_Click(object sender, EventArgs e)
        {
            // Save Blog Settings
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Web, "MetroThemeColour", SiteMetroBrandingDDB.SelectedValue);
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Web, "MetroAccordionActivated", AccordionCheckbox.Checked.ToString());
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Web, "GlobalMetroAccordionType", AccordionTypeDropdown.SelectedValue);
            //Save Global Values
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Web, "InheritMetroThemeColour", InheritThemeFromParent.Checked.ToString());
            if (SPContext.Current.Web.IsRootWeb)
            {
                Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "GlobalMetroAccordionActivated", ApplyAccordionGloballyCB.Checked.ToString());
                Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "GlobalMetroFloatedRibbon", FloatRibbonCB.Checked.ToString());
                Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "GlobalMetroQuickLaunchPosition", QuickLaunchPosition.SelectedValue);
                

            }

            SPUtility.Redirect("settings.aspx", SPRedirectFlags.UseSource | SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Current);
        }

    }
}
